How to get entities from multiple tables with one LINQ call?
How to get entities from multiple tables with one LINQ call?
407
05-Sep-2023
Updated on 06-Sep-2023
Aryan Kumar
06-Sep-2023You can get entities from multiple tables with a single LINQ call using the
Includeoperator. You can load related entities from another table into the current entity using the include operator.The syntax for
Includeoperator is as follows:where
entityis the name of the entity loading the related entitiy andrelatedEntityis the name of the related entity.For example, the following code retrieves all the customers and their orders in a single LINQ call:
This code will first load all the customers from the
Customerstable. Then load all the orders for each customer from theOrderstable.You can also load multiple related entities in a single LINQ call using the include operator. For example, the following code gets all the customers, their orders, and the products that they ordered in a single LINQ call:
This code will first loads all the customers from the
Customerstable. Then, load all the orders of each customer from theOrderstable. Finally, all the products ordered by each customer are loaded from the Products table.